﻿using AME.Database;
using AME.Logger;
using AME.Mapper;
using AME.Repository;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using SqlSugar;
using System.Reflection;

namespace AME.WebAppCore;

public static class StartupExtensions
{
    /// <summary>
    /// 注册服务
    /// </summary>
    /// <param name="builder"></param>
    /// <returns></returns>
    public static WebApplicationBuilder AddAme(this WebApplicationBuilder builder)
    {
        // 在这里配置Builder，比如添加服务
        builder.Services.RegisterServices();

        return builder;
    }


    /// <summary>
    /// 注册自订服务
    /// </summary>
    /// <param name="services"></param>
    /// <returns></returns>
    public static IServiceCollection RegisterServices(this IServiceCollection services)
    {
        //Serilog
        services.UseSerilog();

        //SqlSugar
        services.UseSqlSugar();

        //Mapster
        services.UseMapper();

        //Auto inject
        services.InjectService();  //services.AddTransient(Type,IType);
        services.InjectRepository(); //services.AddScoped(IBaseRepository<Modle>, BaseRepository<Modle>);

        //services.AddJwt<JwtHandler>();
        //services.AddCorsAccessor();

        return services;
    }


    /// <summary>
    /// 配置app
    /// </summary>
    /// <param name="app"></param>
    /// <returns></returns>
    public static IApplicationBuilder RegisterAme(this IApplicationBuilder app)
    {
        app.UseMiddleware<ExceptionHandlingMiddleware>();
        app.UseMiddleware<RequestLoggingMiddleware>();

        return app;
    }

    private static IServiceCollection InjectService(this IServiceCollection services)
    {
        // 获取当前应用程序域中加载的所有程序集
        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

        // 过滤掉以 "System" 或 "Microsoft" 开头的程序集
        var projectAssemblies = assemblies.Where(a => !a.FullName!.StartsWith("System")
                                                    && !a.FullName.StartsWith("Microsoft")
                                                    && !a.IsDynamic); // 排除动态生成的程序集

        // 遍历项目程序集注入服务
        foreach (var assembly in projectAssemblies)
        {
            AutoInjection.AutoDependencyInjection(assembly, services);
        }

        //Application层还没有被调用,无法通过上面加载，Service在这里额外加载
        AutoInjection.AutoDependencyInjection(Assembly.Load("Service"), services);

        return services;
    }

    private static IServiceCollection InjectRepository(this IServiceCollection services)
    {
        // 获取当前应用程序域中加载的所有程序集
        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

        // 过滤掉以 "System" 或 "Microsoft" 开头的程序集
        var projectAssemblies = assemblies.Where(a => !a.FullName!.StartsWith("System")
                                                    && !a.FullName.StartsWith("Microsoft")
                                                    && !a.IsDynamic); // 排除动态生成的程序集

        // 遍历项目程序集注入服务
        foreach (var assembly in projectAssemblies)
        {
            RegisterRepositories.AutoRegisterRepository(assembly, services);
        }

        return services;
    }
}

